package com.ddone.util.mock;

import org.apache.commons.lang3.time.DateUtils;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ddone
 * @date 2024/5/15-09:51
 */
public class MyDateUtil {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2022, 0, 25, 1, 2);
        System.out.println(calendar.getTime());
    }

    public static Date addHours(Date curDate, Integer hour, boolean inWorkTime) {
        Date date = DateUtils.addHours(curDate, hour.intValue());
        int curHour = getHour(date);
        if (inWorkTime) {
            if (curHour < 9) {
                date = DateUtils.addHours(date, 9 - curHour);
            } else if (curHour > 19) {
                date = DateUtils.addHours(date, 33 - curHour);
            }
        }
        return date;
    }

    public static Date initDate(String date, int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        String[] dateStrs = date.split("-");
        int year = Integer.parseInt(dateStrs[0]);
        int month = Integer.parseInt(dateStrs[1]) - 1;
        int dt = Integer.parseInt(dateStrs[2]);
        calendar.set(year, month, dt, hour, minute);
        return calendar.getTime();
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(1);
    }

    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(10);
    }

    public static int getBetweenYear(Date begin, Date end) {
        LocalDate beginL = begin.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endL = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Period p = Period.between(beginL, endL);
        int years = p.getYears();
        return years;
    }
}